package system_code.DeleteFile;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入文件目录:");
        File dirSearch = new File(scanner.next());
        if(!dirSearch.isDirectory()){
            System.out.println("您输入的文件目录不存在!");
            return;
        }
        System.out.print("请输入您要删除的文件名:");
        File toDelete = new File(scanner.next());
        if(!toDelete.isFile()){
            System.out.println("您输入的文件名不存在！");
            return;
        }
        //深度优先遍历查找
        dfs(dirSearch, toDelete);
    }
    public static void dfs(File dirSearch, File toDelete) throws IOException {
        File files[] = dirSearch.listFiles();
        if(files == null){
            return;
        }
        for(File file : files) {
            if(!file.getName().equals(toDelete.getName())){
                dfs(file, toDelete);
            }else{
                //进行删除操作
                deleteFile(toDelete);
            }
        }
    }

    private static void deleteFile(File toDelete) throws IOException {
        System.out.println("你要删除的文件在路径为:" + toDelete.getCanonicalPath() +
                "是否要删除?请输入(Y/N):");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        if(str.equals("Y")){
            toDelete.delete();
            System.out.println("删除成功!");
        }else{
            System.out.println("退出程序");
        }
    }
}
